{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22ab828f",
   "metadata": {},
   "source": [
    "# 第五周Python学习之路"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2af109c7",
   "metadata": {},
   "source": [
    "## 复习上节课内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a6889b75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州首批十大“最具获得感”改革案例出炉\n",
      "0 广\n",
      "1 州\n",
      "2 首\n",
      "3 批\n",
      "4 十\n",
      "5 大\n",
      "6 “\n",
      "7 最\n",
      "8 具\n",
      "9 获\n",
      "10 得\n",
      "11 感\n",
      "12 ”\n",
      "13 改\n",
      "14 革\n",
      "15 案\n",
      "16 例\n",
      "17 出\n",
      "18 炉\n"
     ]
    }
   ],
   "source": [
    "## 字符串的有序性  每个字符串都有索引值（index）（0 1 2 ....）\n",
    "text = \"广州首批十大“最具获得感”改革案例出炉\"\n",
    "print(text)\n",
    "for i,j in enumerate(text):\n",
    "    print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "65e0fd55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text) ##检查字符串总长度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "397be64f",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1.有序的数据结构之列表\n",
    "\n",
    "> 2.用户输入\n",
    "\n",
    "> 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13ec83f4",
   "metadata": {},
   "source": [
    "## 1.引入列表概念\n",
    "> 总结一些通用的有序数据结构的方法\n",
    ">> 1.取值：通过索引值取出对应的内容\n",
    ">> 2.切片：通过索引值区间取出对应的一些内容\n",
    ">> 3.in 可以查看成员对象是否存在\n",
    "\n",
    "> 列表的一些方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a56bc9ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "## 1.引入列表概念\n",
    "网二 = [1,2,3,4,5]\n",
    "print(网二,type(网二))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "eb3920ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['marry', 'kenny', 'jack', 'tonny'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"marry\",\"kenny\",\"jack\",\"tonny\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecd4239f",
   "metadata": {},
   "source": [
    "### 运算法则（in +   切片  取值....）\n",
    "* <font color=\"red\">列表的数据本身不做修改</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "69a78621",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'marry'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出第一位同学名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "df28e846",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看 网三_name_list 有多少位同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7e321413",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三: marry\n",
      "网三: kenny\n",
      "网三: jack\n",
      "网三: tonny\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三:\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8112ee0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 查询数据是否存在\n",
    "\"kenny\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e96c84da",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考可不可以出现重复项： 可以\n",
    "网二_age_list = [18,18,16,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7a9cec9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 16, 20]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0903ecc",
   "metadata": {},
   "source": [
    "### 2.2 列表的方法（function）--对列表数据进行操作\n",
    "\n",
    "\n",
    "> 1.增\n",
    "> 2.删\n",
    "> 3.改\n",
    "> 4.查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecc91b13",
   "metadata": {},
   "source": [
    "### 1. append 增"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "37c76b4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['marry', 'kenny', 'jack', 'tonny']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "58b63b11",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"jay\") \n",
    "## 增加到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "876d9ac1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['marry', 'lihua', 'lihua', 'kenny', 'jack', 'tonny', 'jay', 'eason']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ec8fb910",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"eason\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a4b58487",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['marry', 'kenny', 'jack', 'tonny', 'jay', 'eason']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "cd084862",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list = [\"marry\",\"kenny\",\"jack\",\"tonny\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6df87d1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"lihua\")\n",
    "# 增加元素到某个位置  insert\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7bbaa17d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['marry', 'lihua', 'kenny', 'jack', 'tonny']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "999bbfa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "1ca5cd24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9eae041d",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove(\"kenny\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d06690b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['marry', 'lihua', 'jack', 'tonny']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ac8586a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['marry', 'lihua', 'jack', 'tonny']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和 in 一起使用，先检查成员变量是否存在，再remove\n",
    "if \"kenny\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"kenny\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "1dd78067",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 移除19的元素 \n",
    "age_list = [15,13,14,19,19,18,19,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "6bef45ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思路1： remove，先决条件：知道值出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "c871f6d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 19\n",
      "5 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[15, 13, 14, '19', 18, 20]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop(index)，先决条件：知道出现的位置\n",
    "for i,j in enumerate(age_list):\n",
    "    if j == 19:\n",
    "        print(i,j)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a704fb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课后练习 P68 P81"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
